package com.njtoyo.taxi.admin.rest.controller.business.operation_rule;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.njtoyo.taxi.admin.mapper.master.entity.DriverMarketing;
import com.njtoyo.taxi.admin.rest.wrapper.business.operation_rule.driver_marketing.DriverMarketingAddOrUpdateWrapper;
import com.njtoyo.taxi.admin.rest.wrapper.business.operation_rule.driver_marketing.DriverMarketingQueryWrapper;
import com.njtoyo.taxi.admin.service.business.operation_rule.DriverMarketingService;
import com.taxi.entity.common.RestResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

@RestController
@RequestMapping("business/operation-rule/driver-marketing")
public class DriverMarketingController {


    @Autowired
    private DriverMarketingService driverMarkingRuleService;


    @GetMapping("/list")
    public RestResult list(DriverMarketingQueryWrapper wrapper) {
        return driverMarkingRuleService.list(wrapper);
    }


    @PostMapping("/add")
    public RestResult add(@RequestBody DriverMarketingAddOrUpdateWrapper wrapper) {
        RestResult check = verityForm(wrapper);
        if (ObjectUtil.isNotNull(check)) {
            return check;
        }
        wrapper.setStartDate(wrapper.getStartDate() + " 00:00:00");
        wrapper.setEndDate(wrapper.getEndDate() + " 23:59:59");
        if ("ratio".equals(wrapper.getMode())) {
            wrapper.setRatio(NumberUtil.mul(wrapper.getRatio(), Convert.toBigDecimal("0.01")));
        }
        return driverMarkingRuleService.add(wrapper);
    }

    @PostMapping("/update")
    public RestResult update(@RequestBody DriverMarketingAddOrUpdateWrapper wrapper) {
        RestResult check = verityForm(wrapper);
        if (ObjectUtil.isNull(wrapper.getId())) {
            return RestResult.failed("缺少参数：ID");
        }
        if (ObjectUtil.isNotNull(check)) {
            return check;
        }
        wrapper.setStartDate(wrapper.getStartDate() + " 00:00:00");
        wrapper.setEndDate(wrapper.getEndDate() + " 23:59:59");
        if ("ratio".equals(wrapper.getMode())) {
            wrapper.setRatio(NumberUtil.mul(wrapper.getRatio(), Convert.toBigDecimal("0.01")));
        }
        return driverMarkingRuleService.update(wrapper);
    }


    @DeleteMapping("/delete/{id}")
    RestResult deleteById(@PathVariable("id") Long id) {
        if (ObjectUtil.isNull(id)) {
            return RestResult.failed("缺少必要参数:ID");
        }
        return driverMarkingRuleService.deleteById(id);
    }


    @PostMapping("/updateActive")
    RestResult deleteById(@RequestBody DriverMarketing driverMarketing) {
        if (ObjectUtil.isNull(driverMarketing.getId())) {
            return RestResult.failed("缺少必要参数:ID");
        }
        return driverMarkingRuleService.updateActive(driverMarketing);
    }


    @GetMapping("/info/{id}")
    RestResult getInfoById(@PathVariable("id") Long id) {
        if (ObjectUtil.isNull(id)) {
            return RestResult.failed("缺少必要参数:ID");
        }
        return driverMarkingRuleService.getInfoById(id);
    }

    private RestResult verityForm(DriverMarketingAddOrUpdateWrapper wrapper) {
        if (StrUtil.isBlank(wrapper.getTitle())) {
            return RestResult.failed("请填写标题");
        } else if (ObjectUtil.isNull(wrapper.getDriverRegionId())) {
            return RestResult.failed("请选择司机运营区");
        } else if (StrUtil.isBlank(wrapper.getStartDate()) || StrUtil.isBlank(wrapper.getEndDate())) {
            return RestResult.failed("请选择起始时间");
        } else if (StrUtil.isBlank(wrapper.getMode())) {
            return RestResult.failed("请选择奖励模式");
        } else if (StrUtil.isNotBlank(wrapper.getMode())) {
            if ("fixed".equals(wrapper.getMode()) && ObjectUtil.isNull(wrapper.getFixed())) {
                return RestResult.failed("请填写固定金额");
            } else if ("ratio".equals(wrapper.getMode()) && ObjectUtil.isNull(wrapper.getRatio())) {
                return RestResult.failed("请填写比例");
            }
        }
        if (wrapper.getTimeQuantum().length == 0 || StrUtil.isBlank(wrapper.getTimeQuantum()[0].getStartTime()) ||
                StrUtil.isBlank(wrapper.getTimeQuantum()[0].getEndTime())) {
            return RestResult.failed("请填写时间段");
        }
        return null;
    }

}
